// ~ CopyRight © 2012 USTC SINOVATE  SOFTWARE CO.LTD All Rights Reserved.
package com.cmcc.zysoft.infocollection.mobile.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cmcc.zysoft.infocollection.mobile.dao.SystemUserDao;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.cmcc.zysoft.infocollection.util.MD5Tools;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author yan.dou
 * @date 2012-11-18 下午3:05:05
 */

@Service
public class SystemUserService extends BaseServiceImpl<SystemUser, String> {
	
	@Resource
	private SystemUserDao systemUserDao;
	
	/**
	 * 属性名称: mDeviceService.
	 * 类       型: MDeviceService.
	 */
	@Resource
	private MDeviceService mDeviceService;
	
	/**
	 * 属性名称: mInterfaceLogService.
	 * 类       型: MInterfaceLogService.
	 */
	@Resource
	private MInterfaceLogService mInterfaceLogService;
	
	/**
	 * 属性名称: mEmployeeService.
	 * 类       型: MEmployeeService.
	 */
	@Resource
	private MEmployeeService mEmployeeService;
	
	@Override
	public HibernateBaseDao<SystemUser, String> getHibernateBaseDao() {
		return this.systemUserDao;
	}

	/**
	 * @param userName
	 * @return
	 */
	public String getEmpIdByName(String userName){
		List<SystemUser> list = this.findByNamedParam("userName", userName);
		if(list.size()>0){
			return list.get(0).getEmployeeId().toString();
		}else{
			return "";
		}
	}
	
	/**
	 * @param userName
	 * @return
	 */
	public String getIdByName(String userName){
		List<SystemUser> list = this.findByNamedParam("userName", userName);
		if(list.size()>0){
			return list.get(0).getUserId().toString();
		}else{
			return "";
		}
	}
	
	/**
	 * 手机接口 登陆
	 * @param userCode
	 * @param pass
	 * @return
	 */
	public Map<String,Object> checkLogin(String employeeId,String userCode, String pass) {
		return this.systemUserDao.checkLogin(employeeId,userCode, pass);
	}
	
	/**
	 * 通过用户名、密码 查找人员ID
	 * @param userCode
	 * @param pass
	 * @return
	 */
	public String getEmpIdFromSys(String userCode, String pass){
		SystemUser systemUser = this.systemUserDao.getEmpIdFromSys(userCode,pass);
		if(systemUser == null){
			return "";
		}else{
			return systemUser.getEmployeeId();
		}
	}
	
	/**
	 * 根据登录用户userID 获取公司ID
	 */
	public String getCompanyIdByUserId(String userId){
		SystemUser systemUser = this.systemUserDao.getCompanyIdByUserId(userId);
		if(systemUser == null){
			return "";
		}else{
//			System.out.print("companyId = "+systemUser.getCompany().getCompanyId()+"\n");
//			return systemUser.getCompany().getCompanyId();
			return systemUser.getCompanyId();
		}
	}
	
	/**
	 * 根据登录用户employee_ID 获取公司ID
	 */
	public String getCompanyIdByEmpId(String empId){
		SystemUser systemUser = this.systemUserDao.getCompanyIdByEmpId(empId);
		if(systemUser == null){
			return "";
		}else{
//			return systemUser.getCompany().getCompanyId();
			return systemUser.getCompanyId();
		}
	}
	
	/**
	 * 根据userCode获取SystemUser实体
	 * @param userCode
	 * @return
	 * writer by wmliu
	 */
	public SystemUser getSystemByuserCode(String userCode){
		SystemUser systemUser = this.systemUserDao.getSystemByuserCode(userCode);
		if(systemUser == null){
			return null;
		}else{
			return systemUser;
		}
	}

	public String getUserIdByEmployeeId(String empId) {
		SystemUser systemUser = this.systemUserDao.getCompanyIdByEmpId(empId);
		if(systemUser == null){
			return "";
		}else{
			return systemUser.getUserId();
		}
	}

	/**
	 * 手机侧登陆处理
	 * @param userCode 用户名
	 * @param userPass 密码
	 */
	public SystemUser loginPhoneCheck(String userName, String password) {
		//根据登陆用户名获取登录信息
		SystemUser systemUser = this.systemUserDao.getSystemByuserCode(userName);
		return systemUser;
	}

	/**
	 * 手册登录验证
	 * @param userCode
	 * @param userPass
	 * @return
	 */
	public Map<String, Object> loginPhone(String userName, String password) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userName", userName);
		SystemUser systemUser = this.loginPhoneCheck(userName,password);
		//如果用户名存在，验证密码
		if(systemUser != null){
			//获取当前登录人所在公司ID
//			map.put("companyId", systemUser.getCompany().getCompanyId());
			map.put("companyId", systemUser.getCompanyId());
			map.put("userId", systemUser.getUserId());
			String passSalt = systemUser.getPassSalt();
			//登录用户名存在，验证密码是否正确
			String passWord = MD5Tools.encodePassword(password, passSalt);
			if(passWord.equals(systemUser.getPassword())){
				//验证成功
				List<Map<String,Object>> list = this.systemUserDao.getLoginMessager(systemUser);
				map.put("Rows", list.get(0));
				map.put("Total", "1");
				map.put("success", true);
			}else{
				map.put("Rows", "登录失败");
				map.put("Total", "0");
				map.put("type", "BadCredentials");
				map.put("success", false);
			}	
		}else{
			map.put("Rows", "登录失败");
			map.put("Total", "0");
			map.put("type", "UsernameNotFound");
			map.put("success", false);
		}
		return map;
	}
	
	
	
	
	
	
	
	
	//================================================== 以下方法为新增方法 ==================================================
}